﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
     [MobileAPI(Code = "MC0106", Name = "用户查找商家列表(定位)(分页)")]
    public class USearchShopListByNameAndLocation : BaseAPI, IProcessMessage
    {
        private BLLUShops bllShop;
        protected BLLUShops BLLShop
        {
            get
            {
                if (bllShop == null)
                {
                    bllShop = BLLFactory.getInstance().CreateUShopsBLL();
                }
                return bllShop;
            }
        }
        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string ShopType = reqMsg.Body.GetFieldValue("ShopType");
                string ShopName = reqMsg.Body.GetFieldValue("ShopsName");
                int PageNo = reqMsg.Head.PageNo;
                int PageSize = reqMsg.Head.PageSize;
                double Latitude = reqMsg.Body.GetFieldValue("Latitude").TDouble();
                double Longitude = reqMsg.Body.GetFieldValue("Longitude").TDouble();
                int SortField = reqMsg.Body.GetFieldInt32("SortField"); ;
                int SortType = reqMsg.Body.GetFieldInt32("SortType"); ;
                if (String.IsNullOrEmpty(ShopType) || String.IsNullOrEmpty(ShopName)||Latitude == 0 || Longitude == 0 || PageNo == 0 || PageSize == 0||SortField==0||SortType==0)
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }
                
                List<EShopsInfo> list = (List<EShopsInfo>)BLLShop.SearchShopListByNameLocation(ShopType,ShopName,PageNo,PageSize,Latitude,Longitude,SortField,SortType);
                if (list == null)
                {
                    rspMsg.Head.RspCode = MessageCode.Result_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Result_Empty.Msg;

                    return rspMsg;
                }

                for (int i = 0; i < list.Count; i++)
                {
                    var fll = rspMsg.Body.AddFieldList("Shop");
                    fll.AddField("ShopsId", list[i].ShopsId.TString());
                    fll.AddField("CouponNum", list[i].CouponNum.TString());
                    fll.AddField("ShopsName", list[i].ShopsName.TString());
                    fll.AddField("ShopsType", list[i].ShopsType.TString());
                    fll.AddField("ShopsPraise", list[i].ShopsPraise.TString());
                    fll.AddField("ShopsPoor", list[i].ShopsPoor.TString());
                    fll.AddField("Consumption", list[i].Consumption.ToString("F1"));
                    fll.AddField("RecommendedDishes", list[i].RecommendedDishes.TString());
                    fll.AddField("DeliveryDistance", list[i].DeliveryDistance.TString("F1"));
                    fll.AddField("Distance", list[i].Distance.TString("F1"));
                    fll.AddField("LowestPrice", list[i].LowestPrice.TString("F1"));
                    fll.AddField("Freight", list[i].Freight.TString("F1"));
                    fll.AddField("OpeningTime", list[i].OpeningTime.ToShortTimeString());
                    fll.AddField("ClosingTime", list[i].ClosingTime.ToShortTimeString());
                }
                
                rspMsg.Head.PageNo = PageNo;
                rspMsg.Head.PageSize = PageSize;

                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("USearchShopListByNameAndLocation" + e);
                throw e;
            }
        }

        #endregion
    }
}